package com.txzt.gaea.upgrade.project.service.impl;

import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.ProgressStatus;
import com.txzt.gaea.common.util.DateUtil;
import com.txzt.gaea.common.util.StringUtil;
import com.txzt.gaea.upgrade.project.domain.model.ProjectProgress;
import com.txzt.gaea.upgrade.project.service.IProjectProgressService;
import com.txzt.gaea.vo.AdminUserVO;
import org.beetl.sql.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProjectProgressServiceImpl extends BaseServiceImpl<ProjectProgress> implements IProjectProgressService {

    @Override
    public List<ProjectProgress> selectProjectProgressByProjectId(Long projectId) {
        return sqlManager.lambdaQuery(ProjectProgress.class).andEq("project_id",projectId).asc("id").select();
    }

    @Override
    public int finishProjectProgress(ProjectProgress projectProgress) {
        //数据校验
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        if (null == projectProgress.getEndTime()) {
            return 0;
        }
        Date date = new Date();
        ProjectProgress progress = new ProjectProgress();
        progress.setId(projectProgress.getId());
        progress.setWorkCompletion(projectProgress.getWorkCompletion());
        progress.setAttachment(projectProgress.getAttachment());
        if (projectProgress.getEndTime().getTime() >= date.getTime()) {
            progress.setProgressStatus(ProgressStatus.DONE.getCode());
        }else {
            progress.setProgressStatus(ProgressStatus.DELAY.getCode());
        }
        progress.setFinishBy(adminUserVO.getId());
        progress.setFinishTime(date);
        progress.setUpdateBy(adminUserVO.getId());
        progress.setUpdateTime(date);
        if(!StringUtil.isBlank(projectProgress.getReason())){
            progress.setReason(projectProgress.getReason());
        }
        return sqlManager.updateTemplateById(progress);
    }

    @Override
    public void changeProgressStatus(){
        sqlManager.update("projectProgress.updateProgressStatus");
    }
}
